#include <iconv.h>
#include <string.h>

int UTF8ToGB2312(char* szSrc, size_t iSrcLen, char* szDst, size_t iDstLen)
{
          iconv_t cd = iconv_open("gb2312//IGNORE", "utf-8//IGNORE");   // take care of "//IGNORE", it will ignore those invalid code
          if(0 == cd)
             return -2;
          memset(szDst, 0, iDstLen);
          char **src = &szSrc;
          char **dst = &szDst;
          if(-1 == (int)iconv(cd, src, &iSrcLen, dst, &iDstLen))
             return -1;
          iconv_close(cd);
          return 0;
}

int GB2312ToUTF8(char* szSrc, size_t iSrcLen, char* szDst, size_t iDstLen)
{
          iconv_t cd = iconv_open("utf-8//IGNORE", "gb2312//IGNORE");
          if(0 == cd)
             return -2;
          memset(szDst, 0, iDstLen);
          char **src = &szSrc;
          char **dst = &szDst;
          if(-1 == (int)iconv(cd, src, &iSrcLen, dst, &iDstLen))
             return -1;
          iconv_close(cd);
          return 0;
}